/* Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Account;
import edu.gatech.team404.AccountType;
import edu.gatech.team404.dal.AccountDAL;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class DeleteAccount
 */
@WebServlet("/DeleteAccount")
public class DeleteAccount extends HttpServlet {
	/** Serial Number */
	private static final long serialVersionUID = 1L;
	
	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final HttpSession session = request.getSession(true);
        final PrintWriter out = response.getWriter();
        final String statusTag = "accountDeleteStatus";
        
        final AccountDAL dal = new AccountDAL();
        final String courseName = request.getParameter("courseName");
        
        try {
           final String username = request.getParameter("username");
           boolean isValid = true;
           
           //Validate username
           if (username.isEmpty()) {
        	   isValid = false;
        	   session.setAttribute(statusTag, 
        			   StatusCode.ACCOUNT_DELETE_INVALID_DATA);
           }

           //Delete account
           if (isValid) {
	           final boolean acctCheck = dal.removeAccount(username);
	           if(acctCheck) {
	        	   session.setAttribute(statusTag, StatusCode.ACCOUNT_DELETE_SUCCESS);
	           }
	           else {
	        	   session.setAttribute(statusTag, StatusCode.ACCOUNT_DELETE_FAIL);
	           }
           }
        }
        finally {
        	session.setAttribute("feed", StatusCode.DELETE.toString());
        	if (!courseName.equalsIgnoreCase("null")) {
        		response.sendRedirect("account.jsp?course=" 
            			+ URLEncoder.encode(courseName, "UTF-8"));
        	}
        	else {
        		response.sendRedirect("account.jsp");
        	}
        	dal.close();
        	out.close();
        }
    }
	
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		 processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		 processRequest(request, response);
	}

}
